//
//  genderPickerView.m
//  YOYOLive
//
//  Created by 王根强 on 2016/10/28.
//  Copyright © 2016年 王根强. All rights reserved.
//

#import "PickerView.h"

@interface PickerView ()<UIPickerViewDataSource,UIPickerViewDelegate>{
    NSString * genderStr;
}
/**
 *  pickerview
 */
@property (strong, nonatomic) UIPickerView *pickerView;
@property (nonatomic,strong) UIToolbar *myToolBar;
/**
 *  存放数据的数组
 */
@property (nonatomic,strong)NSArray *genderArray;
@end

@implementation PickerView

- (id) initWithFrame:(CGRect)frame TitleArray:(NSArray *)titleArray {
    if (self = [super initWithFrame:frame]) {
        [self initPickerView:titleArray];
    }
    return self;
}

- (void) initPickerView:(NSArray *)titleArray {
    self.genderArray = titleArray;
    //遮罩层
    UIView *view_cover = [[UIView alloc] initWithFrame:self.frame];
    [self addSubview:view_cover];
    view_cover.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    
    //手势移除
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissView)];
    [view_cover addGestureRecognizer:gesture];
    
    _pickerView=  [[UIPickerView alloc]initWithFrame:CGRectMake(0, kGScreenHeight + 34, kGScreenWidth, 180)];
    _pickerView.showsSelectionIndicator=YES;
    _pickerView.dataSource = self;
    _pickerView.delegate = self;
    _pickerView.contentMode = UIViewContentModeScaleToFill;
    _pickerView.backgroundColor = [UIColor whiteColor];
    [view_cover addSubview:_pickerView];
    
    self.myToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, kGScreenHeight, kGScreenWidth, 34)];
    self.myToolBar.backgroundColor = [UIColor whiteColor];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 54, 34);
    [btn setTitleColor:self.tintColor forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(finishAction) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"确定" forState:UIControlStateNormal];
    [btn setTitleColor:NAV_COLOR_BLUE forState:(UIControlStateNormal)];
    btn.titleLabel.font = kFont14;
    UIBarButtonItem *finishBtn=[[UIBarButtonItem alloc]initWithCustomView:btn];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(0, 0, 54, 34);
    [btn1 setTitleColor:self.tintColor forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    [btn1 setTitle:@"取消" forState:UIControlStateNormal];
    [btn1 setTitleColor:NAV_COLOR_BLUE forState:(UIControlStateNormal)];
    btn1.titleLabel.font = kFont14;
    UIBarButtonItem *cancelBtn=[[UIBarButtonItem alloc]initWithCustomView:btn1];
    UIBarButtonItem *flexible=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    NSArray *btnArr=[[NSArray alloc]initWithObjects:cancelBtn,flexible,flexible,finishBtn, nil];
    [self.myToolBar setItems:btnArr];
    [view_cover addSubview:self.myToolBar];
    
    [UIView animateWithDuration:0.3 animations:^{
        _pickerView.frame = CGRectMake(0, kGScreenHeight - 180, kGScreenWidth, 180);
        self.myToolBar.frame = CGRectMake(0.0, kGScreenHeight - 180 - 34, self.frame.size.width, 34);
    }];
    [self pickerView:self.pickerView didSelectRow:0 inComponent:0];
}

-(void)cancelAction{
    if (self.removeView) {
        self.removeView(@"");
    }
}

-(void)finishAction{
    if (self.removeView) {
        self.removeView(genderStr);
    }
}

#pragma mark --uipickerviewdatasource中的方法
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
        return self.genderArray.count;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return self.genderArray[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSInteger gender = [_pickerView selectedRowInComponent:0];
    genderStr = [self.genderArray objectAtIndex:gender];
}

//重写方法
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:17]];
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

- (void)dismissView {
    if (self.removeView) {
        self.removeView(genderStr);
    }
}

@end
